import platform
import os

# required by get_ip_address
import socket
import fcntl
import struct

from classes import *

def log(*args): # pragma: no cover
    if __debug__:
        print " ".join(str(arg) for arg in args)

    open("log.txt", "at").write("%s\n" % " ".join(str(arg) for arg in args))

# Clear the log file
open("log.txt", "wt")

def clear_screen(): # pragma: no cover
    '''
    Sadly, the part where they said Python was portable was only half the truth.
    '''
    return False
    if platform.system() == "Windows":
        os.system("cls")
    else:
        os.system("clear")


def get_input(input_source = None, extra_params = {}): # pragma no cover
    if not input_source:
        input = raw_input('''Type "help" for help:\nIssue command: ''')
    else:
        input = input_source.get_input(extra_params)
    return input.split(" ")
    
def create_dummy_players():
    return [Player("Gosho"), Player("Ivan")]

def create_dummy_cards(num_chars = 10, num_modifiers = 30, num_deaths = 10):
    chars = []
    modifiers = []
    deaths = []
    for x in range(num_chars):
        chars.append(Character("CharTest%d" % x))

    for x in range(num_modifiers):
        mods = []
        for y in range(Card.num_modifiers):
            if random.randint(0,10) >= 4:
                mods.append(random.randint(-5,3) * 5)
            else:
                mods.append(None)
        modifiers.append(Modifier("ModTest%d" % x, mods))
    
    for x in range(num_deaths):
        mods = []
        for y in range(Card.num_modifiers):
            if random.randint(0,10) >= 9:
                mods.append(random.randint(-5,3) * 5)
            else:
                mods.append(None)
        deaths.append(UntimelyDeath("DeathTest%d" % x, mods))

    return chars, modifiers, deaths

help_message = '''
You can enter commands either as full words, or just as their first letter.
(h)elp  - Displays this screen.
(p)lace/(p)lay card_number target_card_number player_number. Places one of the cards in your hand at the chosen card. place 1 3 2 will place your first card on the third card of player 2.
(d)iscard card_number. Discards a card from your hand.
(e)nd turn. Immediately ends your turn.
'''

def print_data(): # pragma no cover
    '''
    A poor man's Graphical interface.
    '''
    print "It's player %s's turn" % (gs.active_player + 1)
    print "Moves remaining: %s" % (gs.num_moves_per_turn-gs.moves_made)
    print "%s cards remaining in the deck" % len(gs.deck.cards)
    print "%s cards discarded" % len(gs.deck.discarded_cards)
    print gs.players[gs.active_player]
    for id, card in enumerate(gs.players[gs.active_player].cards):
        print id+1, card
    for player in gs.players:
        print player
        for id, character in enumerate(player.characters):
            print id+1, character
    if gs.print_help:
        gs.print_help = False
        print help_message

def get_ip_address(ifname):
    '''
    Taken from:
    http://code.activestate.com/recipes/439094/

    TODO: Determine on which interface the check should be made.
    In Linux it should be something like eth0.
    '''
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])
